package com.robot.backend.service.impl.robot;

import ch.qos.logback.core.util.FileUtil;
import com.robot.backend.mapper.RobotMapper;
import com.robot.backend.pojo.Robot;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.robot.backend.service.robot.robotTestService;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;

@Service
public class robotTestServiceImpl implements robotTestService{
    @Autowired
    RobotMapper robotMapper;
    @Override
    public String startTest(int uid, int bid) {
        return "s";
    }

    @Override
    public String writeTest(String code) {
        try {
            Files.writeString(Path.of("/www/wwwroot/编译执行cpp/main.cpp"), code);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        ProcessBuilder processBuilder = new ProcessBuilder("sh", "/www/wwwroot/编译执行cpp/getRet.sh");
        processBuilder.redirectErrorStream(true);  // 合并标准输出和错误流

        try {
            Process process = processBuilder.start();
            // 读取输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            int exitCode = process.waitFor();
        } catch (IOException | InterruptedException e) {
            System.out.println("编译错误");
            e.printStackTrace();
        }
        return "success";
    }
}
